Creating a Multi-tenant Connection

Description

A multi-tenant connection string can be used to build Software-as-a-Service (SaaS) applications in Alpha Anywhere with ease.

Discussion

When you create a multi-tenant SaaS application (using any development platform, not specifically Alpha Anywhere), you can either give each tenant their own database, or you can use a shared database for all tenants.

Giving each tenant their own database will work well if you do not have a large number of tenants. However, if your application has a large number of tenants, the only practical approach is to have a single database for the application that all tenants share.

If you use a shared database, you will need to have a 'tenant id' field in each table so that the records in the table can be associated with the correct tenant. Every time your code executes a SQL CRUD statement, you will need to remember to include the tenant id field in the SQL statement.

The Multi-tenant driver is used in cases where you have a shared database for all of the tenants. The multi-tenant driver will automatically inject the tenant id field into the SQL so you do not have to remember to do it.

For example, assume you have defined a multi-tenant driver with these properties

Property
Value
Connection string

::Name::northwind

TenantID field name

tenantid

Session variable for tenant ID

tenantid

Data type for TenantID field name

numeric

Now, assume you execute the following SQL query:

SELECT * from Employees

The SQL statement that will be sent to the database will actually be (assuming that the value of session.mytenantid is 23) :

SELECT * from Employees WHERE tenantId = 23

In the case of an INSERT statement, the tenant Id will automatically be set to the value of the current tenant.

For example, your SQL statement:

INSERT into Employees (firstname, lastname) values (:fname, :lname)

will automatically be converted to:

INSERT into Employees (firstname, lastname, tenantid) values (:fname, :lname, 23)

If there are some tables in your multi-tenant SaaS application that are shared by all tenants (i.e. the tables do not have a tenant ID field), then when you query these tables you will not use the multi-tenant connection string. Instead you will use the base connection string.

How to Create a Multi-tenant Connection String

To create a Multi-tenant connection string, create a new connection string (from the Tools > AlphaDAO Connection strings menu on the Web Project Control Panel.)

The Select API dialog will appear when you create a new connection. You can either select the MultiTenant option, or the Custom option. Select the Custom option if you want to customize the Xbasic class that implements the Multi-tenant driver.

images/createmultitenant.jpg
You can also click the smart field button for the Connection Type in the Create SQL Connection String dialog to change the connection type.

If you select the MultiTenant option, the next dialog allows you to set the properties for your multi-tenant connection string.

images/multitenant9.jpg

The properties you must set are:

Property
Description
Connection string

This is the connection string that points to your database.

Tenant ID field name

The name of the tenant ID field in each of the multi-tenant tables in your database (e.g. tenantid)

Session variable name

The name of the session variable that will contain the current tenant ID value (this value will typically be set at the time the tenant logs into the application). For example: session.tenantid. IMPORTANT: Session variables are deleted when a session expires. Therefore you should ensure that the user's login also expires when the session expires.

Tenant ID field type

The data type of the tenant ID field - can either be character or numeric.

Default session variable value

An optional default value to use if the session variable cannot be found. Setting a default value is useful when testing your application in Working Preview.

If you select the Custom option, you will get this dialog where you must specify the name of the Xbasic class that implements the custom driver. Click the Show Example... hyperlink to get an example Xbasic class that implements a multi-tenant driver.

images/multitenant2.jpg
images/multitenant3.jpg

Then select the Xbasic class that you created from the examples and fill in the property sheet, as shown below.

images/multitenant4.jpg

See Creating Custom Connection Strings to learn more.

Videos

How to Create a Multi-tenant Connection String

When you build multi-tenant SaaS applications that use a shared database, each table in the database must have a tenant id field and all of your SQL queries must include the tenant Id. When you use a multi-tenant connection string, the tenant id is automatically injected into all SQL statements before the statement is sent to the database. This makes it easier to build multi-tenant SaaS applications, or to convert an existing application to a multi-tenant application because you do not have to manually adjust all of your SQL statements.

In this video we show how a multi-tenant connection string is defined and then we show the results when a SQL SELECT and INSERT statement are executed.

2018-10-21

See Also